package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.pojo.req.TagCreateDTO;
import com.electromagnetic.industry.software.manage.service.EdTagLibraryService;
import com.electromagnetic.industry.software.manage.service.FileTagRelationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/data/ed/tag")
public class EdTagLibraryController {

    @Resource
    private EdTagLibraryService edTagLibraryService;

    @Resource
    private FileTagRelationService fileTagRelationService;

    // 新建标签组
    @GetMapping("/createGroup")
    @UserOperation(value = "创建了标签组", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> createTagGroup(@RequestParam String tagName) {
        String createdBy = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edTagLibraryService.createTagGroup(tagName, createdBy));
    }

    // 在标签组下新建标签
    @GetMapping("/createTag")
    @UserOperation(value = "创建了标签", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> createTag(@RequestParam String parentId, @RequestParam String tagName) {
        String createdBy = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edTagLibraryService.createTag(parentId, tagName, createdBy));
    }

    // 拖拽修改排序/分组
    @GetMapping("/updateOrder")
    @UserOperation(value = "修改了标签顺序", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> updateTagOrder(@RequestParam String tagId, @RequestParam String newTagId, @RequestParam String newParentId, @RequestParam Integer newOrderBy) {
        String updatedBy = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edTagLibraryService.updateTagOrder(tagId, newTagId, newParentId, newOrderBy, updatedBy));
    }

    // 发布标签
    @PostMapping("/batchPublish")
    @UserOperation(value = "发布了标签组", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> publishTag(@RequestBody List<String> tagIds) {
        return ElectromagneticResultUtil.success(edTagLibraryService.batchPublishTagGroups(tagIds));
    }

    // 废除标签
    @GetMapping("/delete")
    @UserOperation(value = "废除了标签组/标签", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> deleteTag(@RequestParam String tagId) {
        return ElectromagneticResultUtil.success(edTagLibraryService.deleteTagOrGroup(tagId));
    }

    //获取标签数据
    @GetMapping("/list")
    @UserOperation(value = "查看了标签数据", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> listTags() {
        return ElectromagneticResultUtil.success(edTagLibraryService.listTagsWithGroups());
    }

    // 修改标签组/标签
    @GetMapping("/updateTag")
    @UserOperation(value = "修改了标签组/标签", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> updateTag(@RequestParam String tagId, @RequestParam String tagName) {
        String updatedBy = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(edTagLibraryService.updateTagInfo(tagId, tagName, updatedBy));
    }

    // 查看标签树
    @GetMapping("/tree")
    @UserOperation(value = "查看了标签树", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> listTagTree() {
        return ElectromagneticResultUtil.success(edTagLibraryService.listTagTree());
    }

    // 查看所有标签
    @GetMapping("/listAllTags")
    @UserOperation(value = "查看了所有标签", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> listAllTags() {
        return ElectromagneticResultUtil.success(edTagLibraryService.listAllTags());
    }

    // 批量添加标签到文件
    @PostMapping("/addTagsToFile")
    @UserOperation(value = "批量添加了标签到文件", modelName = UserOperationModuleEnum.TAG)
    public ElectromagneticResult<?> addTagsToFile(@RequestBody TagCreateDTO dto) {
        String createdBy = UserThreadLocal.getUserId();
        return ElectromagneticResultUtil.success(fileTagRelationService.addTagsToFile(dto.getFileId(), dto.getTagIds(), createdBy));
    }
}